<template>
  <div class="dialog">
    <el-dialog
      :title="title"
      :visible.sync="isShowDialogComponent"
      width="50%"
      :before-close="handleClose">
      <el-form
        ref="formRef"
        :model="form"
        label-width="100px"
        :rules="formRules"
        v-loading="loading"
        element-loading-text="拼命加载中....."
        element-loading-spinner="el-icon-loading">
        <el-form-item label="学科名称" prop="SubjectName">
          <el-input v-model="form.SubjectName" :maxLength="20"></el-input>
        </el-form-item>
        <el-form-item label="学科描述" prop="Describe">
          <el-input v-model="form.Describe" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="学科图片">
          <upload-img
            :width="16"
            :height="9"
            @showImage="showImage"
            :judge-id="judgeId"
            :image="form.Image">
          </upload-img>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="handleClose" :disabled="loading">取 消</el-button>
    <el-button type="primary" @click="submitEvent" :disabled="loading">确 定</el-button>
  </span>
    </el-dialog>

  </div>
</template>

<script>
//引入裁剪图片的插件
import UploadImg from '../../../components/base/uploadPictures/UploadImg'
//引入接口类
import BaseSubjectManage from '../../../model/baseSubjectManage'
import ProofRules from '../../../ck/util/proofRules'

export default {
  name: 'AddAndEditDialog',
  components: {
    UploadImg
  },
  props: {
    isShowDialogComponent: Boolean,
    title: String,
    judgeId: Number
  },
  created () {
    this.judgeEditButton()
  },
  data () {
    return {
      loading: false,
      form: {
        SubjectName: '',
        Describe: '',
        Image: ''
      },
      formRules: {
        SubjectName: ProofRules.requiredFieldAndLength('学科名称', 2, 20),
        Describe: ProofRules.requiredFieldAndLength('学科描述', 2, 200)
      },
    }
  },
  methods: {
    handleClose (payload) {
      this.$emit('handleClose', payload)
    },
    submitEvent () {
      this.$refs.formRef.validate(async valid => {
        if (!valid) {
          this.$message.error('请填写表单必填项')
        } else {
          if (this.form.Image === '') {
            return this.$message.error('学科图片为必传项，请上传学科图片')
          } else {
            try {
              this.loading = true
              if (this.judgeId === 0) {
                await BaseSubjectManage.addSubject(this.form)
                this.loading = false
                this.$message.success('添加学科信息成功')
                this.$emit('handleClose', 'submitButton')
              } else {
                this.loading = true
                await BaseSubjectManage.editSubjectInfo(this.judgeId, this.form)
                this.loading = false
                this.$message.success('编辑学科信息成功')
                this.$emit('handleClose', 'submitButton')
              }
            } catch (res) {
              this.loading = false
            }
          }
        }
      })
    },
    showImage (result) {
      this.form.Image = result
    },
    async judgeEditButton () {
      if (this.judgeId !== 0) {
        const res = await BaseSubjectManage.showInfoDetail(this.judgeId)
        this.form = res
      }
    }
  }
}
</script>
